/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.fedoraifz.restrequests;

import org.apache.log4j.Logger;
import org.apache.ws.security.util.Base64;
import org.igfay.jfig.JFig;
import org.igfay.jfig.JFigException;

public class UserCredentials {
    private Logger log = Logger.getLogger(UserCredentials.class);
    private String user;
    private String password;
    private String encodedUser;
    private String encodedPasswd;

    public void setUser(String user) {
	this.user = user;
    }
    public String getUser() {
	return user;
    }
    public void setPassword(String password) {
	this.password = password;
    }
    public String getPassword() {
	return password;
    }

    public void setEncodedUser(String encodedUser) {
	this.encodedUser = Base64.encode(encodedUser.getBytes());
    }
    public String getEncodedUser() {
	return encodedUser;
    }
    public void setEncodedPasswd(String encodedPasswd) {
	this.encodedPasswd = Base64.encode(encodedPasswd.getBytes());
    }
    public String getEncodedPasswd() {
	return encodedPasswd;
    }
    public UserCredentials() {
	try {
	    this.user = JFig.getInstance().getValue("fedoraServer", "user").trim();
	    this.password = JFig.getInstance().getValue("fedoraServer", "password").trim();
	    setEncodedUser(this.user);
	    setEncodedPasswd(this.password);
	} catch (JFigException e) {
	    log.error("Error: ", e);
	}
    }
    public UserCredentials(String user, String passwd) {
	this.user = user.trim();
	this.password = passwd.trim();
	setEncodedUser(this.user);
	setEncodedPasswd(this.password);
    }
    /**
     * Returns a String in the form: Basic encoded_user:encoded_passwd
     * @return
     */
    public String getEncodedCredentials() {
	String authorization = this.user + ":" + this.password;
	StringBuilder credString = new StringBuilder();
	credString.append("Basic ");
	credString.append(Base64.encode(authorization.getBytes()));         
	return credString.toString();
    }
}
